package com.kws.tool.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.core.domain.R;
import com.kws.common.core.enums.ModelOpType;
import com.kws.common.core.sys.Ksys;
import com.kws.common.core.utils.KConstants;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.tool.dto.CreateMeasureToolRq;
import com.kws.tool.dto.QueryMeasureToolRq;
import com.kws.tool.dto.info.QueryMeasureToolInfo;
import com.kws.tool.entity.TbMeasureTool;
import com.kws.tool.mapper.MeasureToolMapper;
import com.kws.tool.service.MeasureIndicatorService;
import com.kws.tool.service.MeasureToolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <b>MeasureToolServiceImpl</b>
 *
 * @description: MeasureToolServiceImpl <br>
 * @date: 2024/11/21 13:09 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class MeasureToolServiceImpl extends ServiceImpl<MeasureToolMapper, TbMeasureTool> implements MeasureToolService {
    private static final Logger logger = LoggerFactory.getLogger(MeasureToolServiceImpl.class);

    @Autowired
    private MeasureIndicatorService measureIndicatorService;


    @Override
    public Page<TbMeasureTool> queryMeasureToolPage(QueryMeasureToolRq rq) {
        Page<TbMeasureTool> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        Page<TbMeasureTool> pageInfo = baseMapper.queryMeasureToolPage(page,rq);
        return pageInfo;
    }

    @Override
    @Transactional
    public R createMeasureTool(CreateMeasureToolRq rq) {
        TbMeasureTool tbMeasureTool = new TbMeasureTool();
        BeanUtil.copyProperties(rq,tbMeasureTool);
        tbMeasureTool.setStatus("1");
        Ksys.setBasicModelData(tbMeasureTool, SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE, ModelOpType.CREATE);
        int insert = baseMapper.insert(tbMeasureTool);
        if (1 == insert){
//            boolean status = measureIndicatorService.createMeasureIndicator(tbMeasureTool.getId(),rq.getMeasureIndicatorList());
//            if (status){
//                return R.ok();
//            }else {
//                return R.fail("创建测算工具失败！");
//            }
            return R.ok();
        }else {
            return R.fail("创建测算工具失败！");
        }
    }

    @Override
    public QueryMeasureToolInfo queryMeasureToolById(Long id) {
        return baseMapper.queryMeasureToolById(id);
    }

    @Override
    @Transactional
    public R updateMeasureTool(QueryMeasureToolInfo info) {
        TbMeasureTool tbMeasureTool = baseMapper.selectById(info.getId());
        BeanUtil.copyProperties(info,tbMeasureTool);
        Ksys.setBasicModelData(tbMeasureTool,SecurityUtils.getStringUserId(), KConstants.DATA_ENABLE,ModelOpType.UPDATE);
        int i = baseMapper.updateById(tbMeasureTool);
        if (1 == i){
            //修改指标数据
            boolean status = measureIndicatorService.updateMeasureIndicator(info.getId(),info.getMeasureIndicatorList());
            if (status){
                return R.ok();
            }else {
                return R.fail("修改测试工具内容失败!");
            }
        }else {
            return R.fail("修改测试工具内容失败!");
        }
    }

    @Override
    public boolean deleteMeasureToolById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateMeasureToolStatusById(Long id, String status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",status);
        updateWrapper.set("update_user",SecurityUtils.getStringUserId());
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
